from invidious import Invidious
import requests
import sys

from plugins.core.plugin import Plugin
from plugins.core.pluginvideo import PluginVideo
from plugins.core.pluginplaylist import PluginPlaylist

sys.path.append("..")
from src.vobjects.item import Item
from src.vobjects.video import Video

from src.history import History

class InvidiousPopular(Plugin):

    def __init__(self, history: History=History()):
        super().__init__(history=history)

        self.name = "Invidious: Popular"
        self.is_query_required = False

        self.flag = "-ip"
        self.full_flag = "--ivpopular"
        self.flag_help = "show popular videos from invidious"
        self.flag_action = "store_true"

    def get_popular(self):
        popular = self.invidious.get_popular()

        items = []
        for video in popular:
            if video == None: continue
            video.convert(Video)
            video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
            inHistory = video.in_history(self.history)
            item = video.to_item(inHistory=inHistory)
            items.append(item)

        return items

    def get_items_by_query(self, query: str=""):
        return list()

    def get_items(self):
        return self.get_popular()

if __name__ == "__main__":
    al = InvidiousPopular()

    items = al.get_items_by_query(query="мастера меча онлайн")
    for item in items:
        print(item.title, item.parent.url)




